/**************************************************************************
	File		: led.c
	Author		: TaoLei Zhang
	Date		: 2018-2-5
***************************************************************************/
	
/**************************************************************************
    								include
***************************************************************************/
#include "common_def.h"
#ifndef __IAP__
#include "eeprom.h"
#endif
#include "led.h"	
#include "stm32f10x.h"
/**************************************************************************
    								macro
***************************************************************************/
	
/**************************************************************************
    								typedef
***************************************************************************/
typedef struct tagLedIf_t
{
	GPIO_TypeDef *port;
	uint16_t pin;
}LedIf_t;

/**************************************************************************
    								variables
***************************************************************************/
const LedIf_t g_LedIf[] = 
{
	{GPIOB, GPIO_Pin_0},
	{GPIOC, GPIO_Pin_7},
	{GPIOC, GPIO_Pin_6},
	{GPIOB, GPIO_Pin_14},
	{GPIOB, GPIO_Pin_13},
	{GPIOB, GPIO_Pin_12}
};	

extern __IO uint32_t LocalTime;

/**************************************************************************
    								functions
***************************************************************************/
void LED_Init(void)
{
	int i = 0;
	
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC, ENABLE);

	for (i = 0; i < DIM(g_LedIf); i++)
   	{
   		GPIO_InitTypeDef GPIO_InitStructure;
   		GPIO_InitStructure.GPIO_Pin = g_LedIf[i].pin;
		GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
		GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
		GPIO_Init(g_LedIf[i].port, &GPIO_InitStructure);
	}
}

void LedOn(LedIndex_e index)
{
	GPIO_SetBits(g_LedIf[index].port,g_LedIf[index].pin);
}

void LedOff(LedIndex_e index)
{
	GPIO_ResetBits(g_LedIf[index].port,g_LedIf[index].pin);
}

void LedReverse(LedIndex_e index)
{
	GPIO_WriteBit(g_LedIf[index].port,g_LedIf[index].pin,!GPIO_ReadOutputDataBit(g_LedIf[index].port,g_LedIf[index].pin));
}

#ifndef __IAP__
void LedProcess(void)
{
	static uint32_t runTime = 0;
	static uint32_t audioInputTime = 0;
	static uint32_t sceneTime = 0;
	
	if (LocalTime - runTime >= 500)
    {
        runTime = LocalTime;
        LedReverse(LED_Sys);
    }

	if (LocalTime - audioInputTime >= 500)
	{
		DSP_Scene_t *pScene = &g_Scene;
		audioInputTime = LocalTime;
		if (pScene->audioInputType == ANALOG_Input)
		{
			LedOn(AUDIO_SRC_LED);
		}
		else
		{
			LedReverse(AUDIO_SRC_LED);
		}
	}

	if (LocalTime - sceneTime >= 500)
    {
    	DSP_Scene_e scene = g_Scene.mode;
		
        sceneTime = LocalTime;
		if (scene == DSP_MODE_A)
		{
			LedOn(SCENE_LED);
		}
		else if (scene == DSP_MODE_B)
		{
			LedReverse(SCENE_LED);
		}
		else
		{
			static uint32_t twiceFlag = 0;
			if ((twiceFlag++ % 2) == 0)
				LedReverse(SCENE_LED);
		}
    }

	
	
}
#endif
